package com.wwwarehouse.resource_center.fragment.resource_efficiency_evaluation;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.adapter.pop.BubblePopAdapter;
import com.wwwarehouse.common.bean.carddesk.CardDeskMessageStableCardBean;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.bean.filter.SeriesBean;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.eventbus_event.DrawerSeriesCompleteEvent;
import com.wwwarehouse.common.fragment.DeskDrawerSeriesFragment;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.ResourceEvaluationBean;
import com.wwwarehouse.resource_center.bean.goods.SerializableMap;
import com.wwwarehouse.resource_center.bean.goods.WebDataBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.fragment.base.MyBaseViewPagerFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Joseph on 2017/10/28.
 *         <p/>
 *         资源效率评估
 */
@Route(path = ResourceConstant.RESOURCE_EFFICIENCY_EVALUATION_INFORMATION)
public class ResourceEfficiencyEvaluationFragment extends MyBaseViewPagerFragment {

    public static final int GET_BUSINESS_UNITS_BY_USERID = 1;
    public static final int RESOURCE_EFFICIENCY_EVALUATION = 2;
    private String mSearchContent;
    private List<FilterBean> mList;
    private int num = 9;
    private Map<String, Object> map;
    private CardDeskMessageStableCardBean.TasksBean tasksBean;
    private List<SeriesBean> mFiltList;
    List<FilterBean> filterBeen = new ArrayList<>();
    List<FilterBean> filterBeen1 = new ArrayList<>();
    private List<CardDeskFunctionResponseBean.BusinessBean> businessBeen;
    private ArrayList<String> businessList;
    private ArrayList selectsCategoryList;   //selects二级品类
    private ArrayList selectsItemList;       //selects商品条码
    private String type = "spu";  //
    private String dataSet = "resEfficiSpu";
    private String column = "categorySearch";
    private ArrayList<String> cateValuesList = new ArrayList<>();  //
    private Dialog mDialog;
    private boolean isFirst = true;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ResourceEfficiencyEvaluationFragment)
            mActivity.setTitle(getString(R.string.resource_efficiency_evaluation));
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    public void onEventMainThread(DrawerSeriesCompleteEvent event) {
        if (peekFragment() instanceof ResourceEfficiencyEvaluationFragment) {
            if (event.isReset()) {
                //重置到前一个状态
                resetStatus();

                //重置
                type = "spu";  //
                dataSet = "resEfficiSpu";
                column = "categorySearch";
                businessList = new ArrayList<>();
                for (int i = 0; i < businessBeen.size(); i++) {
                    businessList.add(businessBeen.get(i).getBuId());
                }
                ((BaseCardDeskActivity) mActivity).hideDrawerLayout();
            } else {
                //完成
                mFiltList = event.getList();
                filterBeen = mFiltList.get(0).getList();
                boolean flag = true;
                for (int i = 0; i < filterBeen.size(); i++) {
                    if (filterBeen.get(i).isSelect()) {
                        if (flag) {
                            businessList = new ArrayList<>();
                            flag = false;
                        }
                        businessList.add(businessBeen.get(i).getBuId());
                    }
                }
                if (flag) {
                    businessList = new ArrayList<>();
                    for (int i = 0; i < businessBeen.size(); i++) {
                        businessList.add(businessBeen.get(i).getBuId());
                    }
                }
                filterBeen1 = mFiltList.get(1).getList();
                if (filterBeen1.get(0).isSelect()) {
                    type = "spu";  //
                    dataSet = "resEfficiSpu";
                    column = "categorySearch";
                } else if (filterBeen1.get(1).isSelect()) {
                    type = "sku";  //
                    dataSet = "resEfficiSku";
                    column = "itemSearch";
                } else {
                    type = "spu";  //
                    dataSet = "resEfficiSpu";
                    column = "categorySearch";
                }
                //请求接口
            }
            request();
        }
    }

    @Override
    public void sortClick(View anchor) {
        PopUpUtils.showBubbleDown(anchor, mActivity, mList, new BubblePopAdapter.OnPopListClickListener() {
            @Override
            public void setOnItemClickListener(int position, View view) {
                mList.get(0).setSelect(true);
                request();
            }
        });
    }

    @Override
    public void cancelClick() {
        super.cancelClick();
        mSearchContent = "";
        cateValuesList = new ArrayList<>();
        request();
    }

    @Override
    public void searchNoRealTimeClick(String text) {
        //调用接口搜索
        mSearchContent = text;
        cateValuesList = new ArrayList<>();
        if (!TextUtils.isEmpty(mSearchContent)) {
            cateValuesList.add(mSearchContent);
        }
        request();
    }

    //过滤
    @Override
    public void filterClick() {
        if (peekFragment() instanceof ResourceEfficiencyEvaluationFragment) {
            ((BaseCardDeskActivity) mActivity).showDrawerLayout();
        }
    }

    @Override
    protected void loadDatas() {
        mCustomScroll.setOnPullListener(null);
        if (!sp.getBooleanValue("resource_layer", false)) {
            View view = View.inflate(mActivity, R.layout.view_layer_dialog, null);
            final ImageView layer1 = (ImageView) view.findViewById(R.id.layer1);
            final ImageView layer2 = (ImageView) view.findViewById(R.id.layer2);
            layer1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    layer2.setVisibility(View.VISIBLE);
                    layer1.setVisibility(View.GONE);
                }
            });
            mDialog = new AlertDialog.Builder(mActivity, R.style.customdialog).setView(view).create();
            mDialog.show();
            layer2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                    sp.putBooleanValue("resource_layer", true);
                }
            });
        }

        selectsCategoryList = new ArrayList();
        selectsCategoryList.add("categoryUkid");
        selectsCategoryList.add("categoryName");
        selectsCategoryList.add("star");
        selectsCategoryList.add("rsEarning");
        selectsCategoryList.add("rsWeight");

        selectsItemList = new ArrayList();
        selectsItemList.add("itemUkid");
        selectsItemList.add("itemName");
        selectsItemList.add("star");
        selectsItemList.add("rsEarning");
        selectsItemList.add("rsWeight");
    }

    @Override
    public void requestDatas() {
        httpPost(ResourceConstant.GET_BUSINESS_UNITS_BY_USERID, null, GET_BUSINESS_UNITS_BY_USERID, false, "");
//        Bundle bundleData = getArguments();
//        if (bundleData!=null){
//            tasksBean = bundleData.getParcelable("data");
//            List<String> businessIds = tasksBean.getBusinessIds();
//        }
        mList = new ArrayList<>();
        FilterBean filterBean = new FilterBean(false, "按收益从高到底排序");
        mList.add(filterBean);
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case RESOURCE_EFFICIENCY_EVALUATION:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    ResourceEvaluationBean evaluationBean = JSON.parseObject(commonClass.getData().toString(), ResourceEvaluationBean.class);
                    int total = evaluationBean.getTotal();
                    if (total <= 0) {
                        if (isFirst) {
                            isFirst = false;
                            mLoadingView.setVisibility(View.VISIBLE);
                            mLoadingView.showEmptyView(true);
                            mLoadingView.setEmptyListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mLoadingView.showProgressView(true);
                                    isFirst = true;
                                    request();
                                }
                            });
                        } else {
                            ShowEmptyView();
                        }
                        return;
                    }
                    isFirst = false;
                    mCustomScroll.setOnPullListener(this);
                    WebDataBean dataBean = new WebDataBean();
                    dataBean.setBusinessId(businessList);
                    Bundle bundle = new Bundle();
                    SerializableMap myMap = new SerializableMap();
                    myMap.setMap(map);//将map数据添加到封装的myMap中
                    bundle.putSerializable("map", myMap);   //type
                    bundle.putParcelable("buId", dataBean);   //businessList
                    bundle.putString("type", type);
                    SetFragmentAndPage(total, num, new ResourceEvaluationChildListFragment().getClass().getName(), bundle);
                }
                break;
            case GET_BUSINESS_UNITS_BY_USERID:
                businessBeen = JSON.parseArray(commonClass.getData().toString(), CardDeskFunctionResponseBean.BusinessBean.class);
                businessList = new ArrayList<>();
                for (int i = 0; i < businessBeen.size(); i++) {
                    filterBeen.add(new FilterBean(false, businessBeen.get(i).getBuName(), businessBeen.get(i)));
                    businessList.add(businessBeen.get(i).getBuId());
                }
                mFiltList = new ArrayList<>();
                SeriesBean seriesBean1 = new SeriesBean();
                seriesBean1.setCount(1);
                seriesBean1.setSingle(false);
                seriesBean1.setTitle("业务单位");
                seriesBean1.setList(filterBeen);
                mFiltList.add(seriesBean1);

                SeriesBean seriesBean2 = new SeriesBean();
                seriesBean2.setCount(2);
                seriesBean2.setSingle(true);
                seriesBean2.setTitle("统计纬度");
                filterBeen1.add(new FilterBean(true, "二级品类"));
                filterBeen1.add(new FilterBean(false, "商品条码"));
                seriesBean2.setList(filterBeen1);
                mFiltList.add(seriesBean2);

                DeskDrawerSeriesFragment deskDrawerMultipleFragment = DeskDrawerSeriesFragment.newInstance(mFiltList);
                ((BaseCardDeskActivity) mActivity).setDrawerLayout(deskDrawerMultipleFragment);
                request();
                break;
        }
    }

    private void request() {
        map = new HashMap<>();
        map.put("dataSet", dataSet);
        map.put("ifCount", "true");
        map.put("page", 1);
        map.put("size", num);
        ArrayList filtersList = new ArrayList();
        Map<String, Object> CategoryMap = new HashMap<>();   //二级类目
        CategoryMap.put("column", column);  //categorySearch
        CategoryMap.put("type", "like");
        CategoryMap.put("values", cateValuesList);
        Map<String, Object> businessMap = new HashMap<>();  //业务单位businessUnitId
        businessMap.put("column", "businessUnitId");  //businessUnitId
        businessMap.put("type", "in");
        businessMap.put("values", businessList);
        if (cateValuesList.size() > 0) {
            filtersList.add(CategoryMap);
        }
        filtersList.add(businessMap);

        ArrayList ordersList = new ArrayList();
        Map<String, String> ordersMap = new HashMap<>();   //收益排序
        ordersMap.put("column", "rsEarning");  //rsEarning
        ordersMap.put("type", "desc");//desc
        ordersList.add(ordersMap);

        map.put("filters", filtersList);
        map.put("orders", ordersList);
        if (TextUtils.equals(type, "spu")) {
            map.put("selects", selectsCategoryList);
        } else {
            map.put("selects", selectsItemList);
        }
        httpPost(ResourceConstant.RESOURCE_EFFICIENCY_EVALUATION, map, RESOURCE_EFFICIENCY_EVALUATION, false, "");
    }

    /**
     * 重置到初始状态
     */
    private void resetStatus() {
        List<SeriesBean> seriesList = new ArrayList<>();

        List<FilterBean> filterList1 = new ArrayList<>();

        for (int i = 0; i < businessBeen.size(); i++) {
            filterList1.add(new FilterBean(false, businessBeen.get(i).getBuName(), businessBeen.get(i)));
        }

        SeriesBean seriesBean1 = new SeriesBean();
        seriesBean1.setCount(1);
        seriesBean1.setSingle(false);
        seriesBean1.setTitle("业务单位");
        seriesBean1.setList(filterList1);
        seriesList.add(seriesBean1);

        List<FilterBean> filterList2 = new ArrayList<>();

        SeriesBean seriesBean2 = new SeriesBean();
        seriesBean2.setCount(2);
        seriesBean2.setSingle(true);
        seriesBean2.setTitle("统计纬度");
        filterList2.add(new FilterBean(true, "二级品类"));
        filterList2.add(new FilterBean(false, "商品条码"));
        seriesBean2.setList(filterList2);
        seriesList.add(seriesBean2);

        DeskDrawerSeriesFragment deskDrawerMultipleFragment = DeskDrawerSeriesFragment.newInstance(seriesList);
        ((BaseCardDeskActivity) mActivity).setDrawerLayout(deskDrawerMultipleFragment);
    }


    @Override
    public void onReLoad(int code) {
        requestDatas();
    }

    @Override
    public void onDownPull() {
        if (isShow) {
            ((BaseCardDeskActivity) mActivity).showSearchTitle();
        } else {
            ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        }
        isShow = !isShow;
    }
}
